<template>
    <!-- 课程价钱管理 -->
    <!-- 杨珅 2023年6月15日19:33:19 -->
    <div>
        <el-card>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-s-home" style="font-weight:bold"></i> 后台管理</el-breadcrumb-item>
                <el-breadcrumb-item><a href="/">课程价钱管理</a></el-breadcrumb-item>
                <el-breadcrumb-item></el-breadcrumb-item>
            </el-breadcrumb>
        </el-card>
        <el-card style="margin-top:20px">
            <div style="background-color:white;">
                <el-input placeholder="请输入标题" v-model="courseTitle" style="width:300px;float:left;margin-left: 15px;">
                    <el-button slot="append" icon="el-icon-search" @click="getPrice()"></el-button>
                </el-input>
                <!-- <el-button type="primary" @click="openadd()" style="margin-left:1050px;">新增</el-button> -->
                <!-- 全查 -->
                <el-table :data="PriceData" style="width: 100%; margin-top: 10px; margin-left: 15px;"
                    @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="imageId" label="图片">
                        <template slot-scope="scope">
                            <el-image style="width: 112px; height: 70px"
                                :src="'http://101.43.223.224:8765/image/getImageById?id=' + scope.row.imageId"></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="courseTitle" label="课程标题">
                    </el-table-column>
                    <el-table-column prop="introduction" label="课程介绍">
                    </el-table-column>
                    <el-table-column prop="feeType" label="收费类型">
                        <template slot-scope="scope"> 
                            {{ String(scope.row.priceType) === "1" ? "免费" : "付费" }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="price" label="课程价钱(元)">   
                    </el-table-column>
                    <el-table-column prop="updateTime" label="最后修改时间">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="300">
                        <template slot-scope="scope">
                            <el-button @click="openupdate(scope.row)"
                            type="primary"  size="small">修改</el-button>
                            <el-button @click="deletePriceById(scope.row.id)"
                            type="primary"  size="small">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <el-pagination :page-sizes="[5, 20, 30, 40]" :page-size="pages.pageSize" background 
                    layout="total, sizes, prev, pager, next, jumper" :total="pages.total" :current-page.sync="pages.pageNum"
                    @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top:20px"></el-pagination>
            </div>
            <!-- 添加修改弹窗 -->
            <el-dialog :visible.sync="updateadd" width="39%" :before-close="handleClose()" style="text-align:left">
                <div slot="title" class="header-title" style="text-align:left;font-weight: bold;color:white;font-size:18px;margin-bottom: 10px;">
                    新增/编辑课程价钱
                </div>
                <el-form stripe style="width: 100%" :model="addfrom">
                   
                    <el-form-item prop="price" width="300px">
                        <span style="font-weight:900;color:rgb(93,142,255)">|</span>
                        <span style="margin-left:20px">课程价钱</span>
                        <el-input style="width:500px;margin-left:20px" v-model="addfrom.price"></el-input>
                    </el-form-item>

                </el-form>
                <div style="text-align:center">
                  <el-button style="background-color:rgb(234,244,255);color:rgb(96,169,255)" @click="addPrice()">确定</el-button>
                  <el-button @click="updateadd = false" style="background-color:rgb(234,244,255);color:rgb(96,169,255)">取消</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>
<script>
export default {
    data() {
        return {
            courseTitle:"",
            selectData: [], //下拉数据
            ids: [],//批量删除id
            //全查
            PriceData: [],
            //分页
            pages: {
                total: 0, //数据总条数
                pageNum: 1, //当前页
                pageSize: 5, //每页几条数据
            },
            //添加/编辑表单
            addfrom: {
                id: "",
                price: "",
            },
            //添加修改弹窗
            updateadd: false,
        };
    },
    mounted() {
        this.getPrice()

    },
    methods: {
        //全查-模糊查
        getPrice() {
            this.$axios
                .get("/price/getPrice", {
                    params: {
                        courseTitle: this.courseTitle,
                        pageNum: this.pages.pageNum, //当前页
                        pageSize: this.pages.pageSize, //每页几条数据
                    }
                })
                .then((res) => {
                    if (res.data.code) {
                        this.PriceData = res.data.data.content;
                        this.pages.total = res.data.data.totalSize;
                    }
                })
                .catch((err) => {
                    this.myMessage("error", err);
                });
        },
        //打开添加弹窗
        openadd() {
            this.addfrom = {}//清空表单数据
            this.selectAuthority()//初始化下拉数据
            this.updateadd = true;
        },
        //打开编辑弹窗
        openupdate(data) {
            this.addfrom = JSON.parse(JSON.stringify(data)); //回显
            this.selectAuthority()//初始化下拉数据
            this.updateadd = true;
        },
        //动态下拉数据
        selectAuthority() {
            this.$axios
                .get("/price/courseSelect", {
                })
                .then((res) => {
                    if (res.data.code) {
                        this.selectData = res.data.data;
                    }
                })
                .catch((err) => {
                    this.myMessage("error", err);
                });
        },
        //添加/编辑
        addPrice() {
            if (!this.addfrom.id) {
                this.$axios
                    .post("/price/addPrice", this.addfrom)
                    .then((res) => {
                        if (res.data.code) {
                            this.myMessage("success", "添加成功");
                            this.getPrice()//跳转全查页面
                            this.updateadd = false
                        }
                    })
                    .catch((err) => {
                        this.myMessage("error", err);
                    });
            } else {
                //修改信息
                this.$axios
                    .put("/price/updatePriceById", this.addfrom)
                    .then((res) => {
                        if (res.data.code) {
                            this.myMessage("success", "修改成功");
                            this.getPrice()//跳转全查页面
                            this.updateadd = false
                        } else {
                            this.myMessage("warning", "修改失败！");
                        }
                    });
            }
        },
        //选择数据数量
        handleSelectionChange(val) {
            this.ids = val.map(v => v.id)
            // this.$message.warning("选择了"+this.ids.length+"条数据");
        },
        //批量删除
        delBath() {
            this.$axios.post("/member/deleteMemberByIds", this.ids).then(res => {
                if (res) {
                    this.$message.success("批量删除成功！");
                    this.getMember()
                } else {
                    this.$message.error("删除失败！");
                }
            })
        },
        //逻辑删除
        deletePriceById(id) {
            this.$confirm('此操作将价格彻底删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.delete('/price/deletePriceById', {
                    params: {
                        id: id
                    }
                }).then(res => {
                    if (res.data.code == 200) {
                        this.myMessage('success', '删除成功')
                        this.getPrice()
                    } else {
                        this.myMessage('warning', '删除失败')
                    }
                }).catch(res => {
                    this.myMessage('error', '删除失败，请联系管理员')
                })
            }).catch(res => {
                this.myMessage('info', '已取消删除')
            })
        },
        myMessage(type, message) {
            this.$message({
                message: message,
                type: type
            })
        },

        //分页
        handleSizeChange(val) {
            this.pages.pageSize = val;
            this.getPrice();
        },
        handleCurrentChange(val) {
            this.pages.pageNum = val;
            this.getPrice();
        },
        //信息
        myMessage(type, message) {
            this.$message({
                message: message,
                type: type,
            });
        },
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
    },
};
</script>
<style >
.el-dialog__header {
  background: rgb(93,142,255);
  text-align: center;
}
</style>